<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <button id="btn">修改obj的值</button>


  <h3>obj的值是:</h3>

  <script>

    let obj={
      name: 123
    }


    Object.defineProperty(obj,'name',{
      get () {
        console.log('获取值的时候 触发')
      },
      set (val) {
        console.log('设置值的时候触发')
        console.log(val)
        document.querySelector('h3').innerHTML=`obj的值是: ${val}`
      }
    })


    btn.onclick=function() {
      obj.name=Math.random()
    }




  </script>
</body>

</html>